package cc.customer.cRound.service.Impl;

import cc.customer.cRound.dao.CRoundConfigMapper;
import cc.customer.cRound.dao.CRoundLevelMapper;
import cc.customer.cRound.domain.CRoundConfig;
import cc.customer.cRound.domain.CRoundGrade;
import cc.customer.cRound.domain.CRoundLevel;
import cc.customer.cRound.service.CRoundConfigService;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.Utils;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class CRoundConfigServiceImpl extends BaseService<CRoundConfig> implements CRoundConfigService {

    private static final Logger logger= LoggerFactory.getLogger(CRoundConfigServiceImpl.class);

    @Autowired
    private CRoundConfigMapper configMapper;
    @Autowired
    private CRoundLevelMapper levelMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public ResponseBo editRoundConfig(CRoundConfig config)throws Exception{
        try {
            /**1：查询当前模板得配置项目**/
            List<CRoundConfig> list = configMapper.selectConfigByPidNEw(config);
            /**获取配置数量**/
            List<String> listCount = Arrays.asList(config.getcCount().split(","));
            List<String> listGroud = Arrays.asList(config.getcGradeId().split(","));

            if (list.isEmpty()){
                /**
                 * 判断是不是为空,如果为空则新增
                 * **/
                CRoundConfig cRoundConfig = null;
                List<CRoundConfig> addlist = new ArrayList<>();//新增
                for(int i = 0; i< listGroud.size();i++){
                    cRoundConfig = new CRoundConfig();
                    cRoundConfig.setcUuid(Utils.getUUID());
                    cRoundConfig.setcGradeId(listGroud.get(i));
                    cRoundConfig.setcCount(listCount.get(i));
                    cRoundConfig.setcNcount(listCount.get(i));
                    cRoundConfig.setcDeptid(config.getcDeptid());
                    cRoundConfig.setcNdeptid(config.getcDeptid());
                    cRoundConfig.setcPid(config.getcPid());
                    cRoundConfig.setcType("1");//无用字段
                    cRoundConfig.setcOrder("1"); //无用字段
                    cRoundConfig.setcYiCount("0");
                    addlist.add(cRoundConfig);
                }
                if(!addlist.isEmpty()){
                    configMapper.insertList(addlist);
                }
            }else{
                /**如果不为空则部门是不是有什么变换**/
                String tdeptid = config.getcDeptid();
                List<String> tdepyids= Arrays.asList(tdeptid.split(","));

                /**定义一个删除的部门**/
                List<String> delDept = new ArrayList<>();
                /**定义删除后的当前轮次循环部门**/
                List<String> listCdept= new ArrayList<>();

                if (!list.isEmpty()){
                    CRoundConfig rConfig = list.get(0);
                    String  cdeptid = rConfig.getcNdeptid(); //查找循环的部门
                    /**判断是不是删除了部门,如果删除了，则删除相应的 配置人数也删除了，其他的不做处理**/
                    List<String> cdeptids = Arrays.asList(cdeptid.split(","));
                    for (String cdept:cdeptids){
                        boolean ble  = true;
                        for (String tdept:tdepyids){
                            if (cdept.equals(tdept)){
                                ble = false;
                                break;
                            }
                        }
                        /**如果不存在进入删除部门,删除人员及时生效**/
                        if (ble){
                            delDept.add(cdept);
                        }
                        listCdept.add(cdept);
                    }
                }
                /**删除后剩余当前部门的信息**/
                listCdept.removeAll(delDept);
                /**删除配置人员信息**/
                CRoundLevel cRoundLevel = null;
                if (!delDept.isEmpty()){
                    for (String dept :delDept){
                        cRoundLevel = new CRoundLevel();
                        cRoundLevel.setlUuid(config.getcPid());
                        cRoundLevel.setlDeptid(dept);
                        levelMapper.delete(cRoundLevel);
                    }
                }
                //当前循环的部门
                String ddept = StringUtils.join(listCdept,",");
                boolean ble = false;
                for (CRoundConfig cRound:list){
                    if (!StringUtils.isEmpty(cRound.getcRdrecorder())){
                        ble = true;
                    }
                }
                /**循环判断是不是出现的当前部门或者轮数的变化**/
                for (CRoundConfig cRound:list){
                    /**无论是不是删除，都要更新下一轮的配置部门**/
                    cRound.setcDeptid(tdeptid);
                    /**如果是删除了，则立即生效删除当前轮次的部门**/
                    if (ble){
                        cRound.setcNdeptid(ddept);
                    }else{
                        cRound.setcNdeptid(tdeptid);
                    }

                    /**更新部门和更新 轮数**/
                    for(int i = 0; i< listGroud.size();i++){
                        String  groudid = listGroud.get(i);
                        if (cRound.getcGradeId().equals(groudid)){
                            /**如果不为空则当前轮数是不是有什么变化**/
                            cRound.setcCount(listCount.get(i));
                        }
                    }
                    //TODO(优化成修改成批量更新)
                    configMapper.updateByPrimaryKeySelective(cRound);
                }
            }
            return ResponseBo.ok("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("配置大搜资源失败");
        }
    }
}
